import userApi from '../../../api/userInfo'
import { upLoad } from '../../../utils/upImgs'
import { toast } from '../../../utils/util'
Page({
  data: {
    userInfo: {},
    showWarnConfirm: false
  },
  onLoad: function (options) {
    this.getUserInfo()
  },
  // 用户信息
  getUserInfo () {
    userApi.getUserInfo().then(res => {
      this.setData({
        userInfo: res.data,
      })
    })
  },
  changeTap (e) {
    let { type } = e.currentTarget.dataset;
    this.data.userInfo[type] = e.detail.value;
    this.setData({
      userInfo: this.data.userInfo
    })
  },
  editUserInfo () {
    this.setData({
      showWarnConfirm: true
    })
    
  },
  okDialog () {
    userApi.editUser(this.data.userInfo).then(res => {
      toast('修改成功')
      this.setData({
        showWarnConfirm: false
      })
      setTimeout(() => {
        wx.navigateBack()
      },1000)
    })
  },
  closeDialog () {
    this.setData({
      showWarnConfirm: false
    })
  },
  avatarClick () {
    upLoad.uploadPic(1).then(res => {
      if (res.errMsg = "chooseImage:ok") {
        this.setData({
          "userInfo.avatar": res[0].url
        })
      }
    })
  }
});